/*
 * *****************************************************************************
 *  Copyright ( c ) 2019 huibur Inc. All Rights Reserved.
 *
 *  This software is the confidential and proprietary information of Huibur Inc
 *  (*Confidential Information*). You shall not disclose such Confidential Information
 *  and shall use it only in accordance with the terms of the license agreement
 *  you entered into with Huibur or a Huibur authorized
 *  reseller (the "License Agreement*).
 *  ******************************************************************************
 */

package com.huibur.devicecheck.controller;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.huibur.devicecheck.model.entity.ExportDeviceEntity;
import com.huibur.devicecheck.model.param.DeviceParam;
import com.huibur.devicecheck.model.po.DeviceCategoryPO;
import com.huibur.devicecheck.model.po.DevicePO;
import com.huibur.devicecheck.model.po.TypePO;
import com.huibur.devicecheck.model.query.DeviceQuery;
import com.huibur.devicecheck.model.vo.DeviceVO;
import com.huibur.devicecheck.request.ListRes;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.service.IDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/*******************************************************
* Title: DeviceController.java
* Description: 
* @author baiqingshuo
* @date 2021/9/23
* @version 1.0
*******************************************************/
@RestController
@RequestMapping("/devices")
@Api(value = "设备管理",tags = {"设备管理"})
@Slf4j
public class DeviceController {

    @Autowired
    IDeviceService iDeviceService;

    @Value("${upload-path}")
    private String uploadPath;

    /**
     * 新增设备
     *
     * @param deviceParam
     * @return
     */
    @PostMapping("")
    @ApiOperation(value = "新增装置", response = ObjRes.class)
    public ObjRes<Void> insertDevice(@RequestBody DeviceParam deviceParam) {
        return iDeviceService.insertDevice(deviceParam);
    }

    /**
     * 批量新建
     * @param deviceParam
     * @return
     */
    @PostMapping("/saveBatchDevice")
    @ApiOperation(value = "批量新建", response = ObjRes.class)
    public ObjRes<Void> saveBatchDevice(@RequestBody DeviceParam deviceParam) {
        return iDeviceService.saveBatchDevice(deviceParam);
    }

    /**
     * 删除设备
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除设备", response = ObjRes.class)
    public ObjRes<Boolean> deleteById(@PathVariable Long id) {
        return new ObjRes<Boolean>().data(iDeviceService.deleteById(id));
    }

    /**
     * 编辑设备
     *
     * @param id
     * @param deviceParam
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "编辑设备", response = ObjRes.class)
    public ObjRes<Void> updateDevice(@PathVariable Long id, @RequestBody DeviceParam deviceParam) {
        return iDeviceService.updateDevice(id, deviceParam);

    }

    /**
     * 设备列表（查询并分页)
     *
     * @param deviceQuery
     * @return
     */
    @GetMapping("")
    @ApiOperation(value = "设备列表(查询并分页)", response = PageRes.class)
    public PageRes<DeviceVO> selectDeviceList(DeviceQuery deviceQuery) {
        return iDeviceService.selectDeviceList(deviceQuery);
    }

    /**
     * 获取设备详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取设备详情", response = ObjRes.class)
    public ObjRes<DeviceVO> selectDeviceById(@PathVariable Long id) {
        return new ObjRes<DeviceVO>().data(iDeviceService.selectDeviceById(id));
    }

    /**
     * 检查项获取设备名称
     *
     * @return
     */
    @GetMapping("/selectDeviceName")
    @ApiOperation(value = "检查项获取设备名称", response = ListRes.class)
    public ListRes<DeviceCategoryPO> selectDeviceName() {
        return new ListRes<DeviceCategoryPO>().data(iDeviceService.selectDeviceName());
    }

    /**
     * 获取生产厂家列表
     *
     * @return
     */
    @GetMapping("/selectDeviceManufacturer")
    @ApiOperation(value = "获取生产厂家列表", response = ListRes.class)
    public ListRes<String> selectDeviceManufacturer() {
        return new ListRes<String>().data(iDeviceService.selectDeviceManufacturer());

    }

    /**
     * 获取设备类别列表
     *
     * @return
     */
    @GetMapping("/selectDeviceType")
    @ApiOperation(value = "获取设备类别列表", response = ListRes.class)
    public ListRes<TypePO> selectDeviceType() {
        return new ListRes<TypePO>().data(iDeviceService.selectDeviceType());
    }

    /**
     * 导入Excel表格并解析入库
     * @param filePath
     * @return
     */
    @PostMapping("/importFile")
    @ApiOperation(value = "导入Excel表格并解析入库", response = ObjRes.class)
    public ObjRes<String> importFile(String filePath) throws ParseException {
        String s = iDeviceService.importFile(uploadPath + filePath);
        if (s.equals("success")) {
            return new ObjRes<String>().data(s);
        } else {
            return new ObjRes<String>().error(s);
        }
    }

//    /**
//     * 导出设备数据
//     *
//     * @return
//     */
//    @GetMapping("/exportDeviceFile")
//    @ApiOperation(value = "导出设备数据", response = ObjRes.class)
//    public void exportDeviceFile(DeviceQuery deviceQuery, HttpServletResponse response) throws Exception {
//        OutputStream os = response.getOutputStream();// 取得输出流
//        String fileName = java.net.URLEncoder.encode("设备数据", "UTF-8") + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + ".xlsx";
//        response.setHeader("Content-disposition", "attachment; filename=" + fileName);// 设定输出文件头
//        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
//        WritableWorkbook workbook = jxl.Workbook.createWorkbook(os); // 建立excel文件
//        WritableSheet sheet1 = workbook.createSheet("设备数据", 0);//第一个sheet名
//
//        sheet1.addCell(new Label(0, 0, "设备类别"));
//        sheet1.addCell(new Label(1, 0, "设备名称"));
//        sheet1.addCell(new Label(2, 0, "设备型号"));
//        sheet1.addCell(new Label(3, 0, "生产厂家"));
//        sheet1.addCell(new Label(4, 0, "购置时间"));
//        sheet1.addCell(new Label(5, 0, "计量单位"));
//        sheet1.addCell(new Label(6, 0, "数量"));
//        sheet1.addCell(new Label(7, 0, "存放地点"));
//
//        List<ExportDeviceEntity> exportDeviceEntities = iDeviceService.exportDeviceFile(deviceQuery);
//        for (int i = 0; i < exportDeviceEntities.size(); i++) {
//            ExportDeviceEntity exportDeviceEntity = exportDeviceEntities.get(i);
//            sheet1.addCell(new Label(0, i + 1, exportDeviceEntity.getDeviceType()));
//            sheet1.addCell(new Label(1, i + 1, exportDeviceEntity.getDeviceName()));
//            sheet1.addCell(new Label(2, i + 1, exportDeviceEntity.getDeviceModel()));
//            sheet1.addCell(new Label(3, i + 1, exportDeviceEntity.getManufacturer()));
//            sheet1.addCell(new Label(4, i + 1, exportDeviceEntity.getPurchaseAt() + ""));
//            sheet1.addCell(new Label(5, i + 1, exportDeviceEntity.getUnit()));
//            sheet1.addCell(new Label(6, i + 1, exportDeviceEntity.getNum().toString()));
//            sheet1.addCell(new Label(7, i + 1, exportDeviceEntity.getStorePlace()));
//        }
//        workbook.write();
//        workbook.close();
//        os.close();
//    }
}


